package com.example.feed.multichannel;

import android.content.Context;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.example.uiframework.utils.Utils;
import com.x.uikit.link.XParams;
import com.x.uikit.channel.feed.AbstractFeedLoadMoreView;
import com.x.uikit.multistateview.MultiStater;

//todo 实现ui
public class ChannelLoadMoreView extends AbstractFeedLoadMoreView {
    private ProgressBar mProgressBar;
    private TextView mTvTips;

    public ChannelLoadMoreView(@NonNull Context context) {
        super(context);
        mProgressBar = new ProgressBar(getContext());
        mProgressBar.setIndeterminate(true);
        LayoutParams lpProgress = new LayoutParams(Utils.res.dpI(32), Utils.res.dpI(32));
        lpProgress.gravity = Gravity.CENTER;
        this.addView(mProgressBar, lpProgress);
        mTvTips = new TextView(getContext());
        mTvTips.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
        mTvTips.setTextColor(0xff888888);
        mTvTips.setGravity(Gravity.CENTER);
        LayoutParams lpTips = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        this.addView(mTvTips, lpTips);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        final int height = Utils.res.dpI(48);
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void updateViewByState(MultiStater.State newState, MultiStater.State oldState, XParams params) {
        mProgressBar.setVisibility(newState == MultiStater.State.LOADING ? VISIBLE : GONE);
        mTvTips.setVisibility(newState != MultiStater.State.LOADING ? VISIBLE : GONE);
        String tips = "";
        switch (newState) {
            case EMPTY:
                tips = "我是有底线的";
                break;
            case ERROR:
                tips = "加载失败，请重试~";
                break;
            case SUCCESS:
                tips = String.format("已为你更新%s条数据", XParams.fetch(params, "num", String.class, "0"));
        }
        mTvTips.setText(tips);
    }
}
